package com.autumn.util.excel.export;

import com.autumn.util.DateUtils;
import com.autumn.util.TimeSpan;
import com.autumn.util.TypeUtils;
import com.autumn.util.excel.column.ColumnInfo;
import com.autumn.util.excel.enums.CellType;
import com.autumn.util.excel.AbstractAdapterInfo;
import com.autumn.util.excel.workbook.WorkbookFactory;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.Date;

/**
 * 导出适配信息
 */
public class ExportAdapterInfo extends AbstractAdapterInfo {

	protected ExportAdapterInfo(int columnIndex, ColumnInfo columnInfo) {
		super(columnIndex, columnInfo);
	}

	/**
	 * 实例化 ExportAdapterInfo 类新实例
	 *
	 * @param columnIndex
	 * @param columnInfo
	 * @param cellStyle
	 */
	public ExportAdapterInfo(int columnIndex, ColumnInfo columnInfo, CellStyle cellStyle) {
		super(columnIndex, columnInfo);
		this.setCellStyle(cellStyle);
	}

	/**
	 * 单元格样式
	 */
	private CellStyle cellStyle;

	/**
	 * 创建标题单元格
	 *
	 * @param sheet
	 *            工作表
	 * @param row
	 *            行
	 * @param titleCellStyle
	 *            标题单元格式
	 * @return
	 */
	public Cell createTitleCell(Sheet sheet, Row row, CellStyle titleCellStyle) {
		ColumnInfo columnInfo = this.getColumnInfo();
		String title = columnInfo.getFriendlyName();
		Cell cell = WorkbookFactory.createTitleCell(row, this.getColumnIndex(), title, titleCellStyle);
		WorkbookFactory.setWidth(sheet, this.getColumnIndex(), columnInfo.getWidth());
		return cell;
	}

	/**
	 * 创建空白单元格
	 *
	 * @param row
	 *            行
	 * @return
	 */
	public Cell createBlankCell(Row row) {
		return WorkbookFactory.createValueCell(row, getColumnIndex(), "", getCellStyle());
	}

	/**
	 * 创建单元格
	 *
	 * @param row
	 *            行
	 * @param value
	 *            值
	 * @return
	 */
	public Cell createValueCell(Row row, Object value) {
		if (value == null) {
			return WorkbookFactory.createValueCell(row, getColumnIndex(), "", getCellStyle());
		}
		switch (getColumnInfo().getCellType()) {
		case CellType.CELL_TYPE_BOOLEAN:
			return WorkbookFactory.createValueCell(row, getColumnIndex(),
					TypeUtils.toConvert(boolean.class, value) ? "是" : "否", getCellStyle());
		case CellType.CELL_TYPE_DATE:
		case CellType.CELL_TYPE_DATETIME:
			return WorkbookFactory.createValueCell(row, getColumnIndex(), TypeUtils.toConvert(Date.class, value),
					getCellStyle());
		case CellType.CELL_TYPE_TIME:
			if (value instanceof TimeSpan) {
				return WorkbookFactory.createValueCell(row, getColumnIndex(), DateUtils.FORMAT_TIME.format(value),
						getCellStyle());
			} else {
				return WorkbookFactory.createValueCell(row, getColumnIndex(), value.toString(), getCellStyle());
			}
		case CellType.CELL_TYPE_DOUBLE:
		case CellType.CELL_TYPE_BIGDECIMAL:
			return WorkbookFactory.createValueCell(row, getColumnIndex(), TypeUtils.toConvert(double.class, value),
					getCellStyle());
		case CellType.CELL_TYPE_INTEGER:
			return WorkbookFactory.createValueCell(row, getColumnIndex(), TypeUtils.toConvert(long.class, value),
					getCellStyle());

		default:
			return WorkbookFactory.createValueCell(row, getColumnIndex(), value.toString(), getCellStyle());
		}
	}

	/**
	 * 合并行
	 *
	 * @param sheet
	 *            工作表
	 * @param firstRow
	 *            首行
	 * @param lastRow
	 *            最后一行
	 */
	public void mergeRow(Sheet sheet, int firstRow, int lastRow) {
		sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, getColumnIndex(), getColumnIndex()));
	}

	/**
	 * 获取单元格样式
	 *
	 * @return
	 */
	public CellStyle getCellStyle() {
		return cellStyle;
	}

	/**
	 * 设置单元格样式
	 *
	 * @param cellStyle
	 */
	private void setCellStyle(CellStyle cellStyle) {
		this.cellStyle = cellStyle;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((cellStyle == null) ? 0 : cellStyle.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (!super.equals(obj)) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		ExportAdapterInfo other = (ExportAdapterInfo) obj;
		if (cellStyle == null) {
			if (other.cellStyle != null) {
				return false;
			}
		} else if (!cellStyle.equals(other.cellStyle)) {
			return false;
		}
		return true;
	}

}
